package com.lwj.liwanjia.view.activity.work.shang_jia;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.WorkBean04;
import com.lwj.liwanjia.bean.WorkBean05;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.SpUtils;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.DingDanHomeActivity;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 我是商家
 * Created by 001 on 2017/9/12.
 */

public class WoShiShangJiaActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            switch (msg.what) {

                //验证中
                case 0:
                    startActivity(new Intent(WoShiShangJiaActivity.this, ShenQingZhongActivity.class));
                    finish();
                    break;
                //验证通过
                case 1:
                    //请求网络 获取数据
                    initShuJu();
                    break;
                //验证失败
                case 2:
                    startActivity(new Intent(WoShiShangJiaActivity.this, ShenQingShiBaiActivity.class));
                    finish();
                    break;
                //未注册
                case 3:
                    startActivity(new Intent(WoShiShangJiaActivity.this, RegisterActivity.class));
                    Toast.makeText(WoShiShangJiaActivity.this, "请先注册", Toast.LENGTH_LONG).show();
                    finish();
                    break;
                case 4:
                    startActivity(new Intent(WoShiShangJiaActivity.this, LoginActivity.class));
                    finish();
                    break;
                //数据申请失败
                case 5:
                    Toast.makeText(WoShiShangJiaActivity.this, "数据获取失败", Toast.LENGTH_LONG).show();
                    break;
                //数据申请成功
                case 6:
                    if (!TextUtils.isEmpty(name01)
                            && !TextUtils.isEmpty(name02)
                            && !TextUtils.isEmpty(telephone)
                            && !TextUtils.isEmpty(address)
                            && !TextUtils.isEmpty(photoUrl)) {

                        Glide.with(WoShiShangJiaActivity.this).load(photoUrl).error(R.mipmap.touxiang).into(circle_photo);
                        tv_name01.setText(name01);
                        tv_name02.setText(name02);
                        tv_telephone.setText(telephone);
                        tv_address.setText(address);
                    }
                    break;


            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private ImageView iv_right;
    private RelativeLayout rl_shangPin;//我的商品
    private RelativeLayout rl_maiJia;//卖家交流
    private RelativeLayout rl_keFu;//客服
    private Dialog keFuDialog;//客服 Dialog
    private TextView tv_number;//客服 电话
    private RelativeLayout rl_yongHuFanKui;//用户反馈
    private RelativeLayout rl_woDeJiLv;//我的记录
    private RelativeLayout rl_pinLun;//评论查看
    private RelativeLayout rl_woDeShouRu;//我的收入
    private RelativeLayout rl_dingDan;//我的订单
    private CircleImageView circle_photo;//头像
    private TextView tv_name01;//商家 名字
    private TextView tv_name02;//商铺名字
    private TextView tv_telephone;//电话
    private TextView tv_address;//地址
    private String name01;
    private String name02;
    private String telephone;
    private String address;
    private String photoUrl;

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        rl_shangPin = (RelativeLayout) findViewById(R.id.rl_woDeShangPin);
        rl_maiJia = (RelativeLayout) findViewById(R.id.rl_maiJiaJiaoLiu);
        rl_keFu = (RelativeLayout) findViewById(R.id.rl_keFu);
        rl_yongHuFanKui = (RelativeLayout) findViewById(R.id.rl_yongHuFanKui);
        rl_woDeJiLv = (RelativeLayout) findViewById(R.id.rl_woDeJiLu);
        rl_pinLun = (RelativeLayout) findViewById(R.id.rl_pingLunChaKan);
        rl_woDeShouRu = (RelativeLayout) findViewById(R.id.rl_woDeShouRu);
        rl_dingDan = (RelativeLayout) findViewById(R.id.rl_dingDan);

        circle_photo = (CircleImageView) findViewById(R.id.circle_photo);
        tv_name01 = (TextView) findViewById(R.id.tv_name01);
        tv_name02 = (TextView) findViewById(R.id.tv_name02);
        tv_telephone = (TextView) findViewById(R.id.tv_telephone);
        tv_address = (TextView) findViewById(R.id.tv_address);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.mipmap.xiaoxi);
        tv_title.setText("我是商家");

        tv_back.setOnClickListener(this);
        iv_right.setOnClickListener(this);
        rl_shangPin.setOnClickListener(this);
        rl_maiJia.setOnClickListener(this);
        rl_keFu.setOnClickListener(this);
        rl_yongHuFanKui.setOnClickListener(this);
        rl_woDeJiLv.setOnClickListener(this);
        rl_pinLun.setOnClickListener(this);
        rl_woDeShouRu.setOnClickListener(this);
        rl_dingDan.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        //客服Dialog
        keFuDialog();

        //获取数据 判断是否注册过
        initOkHttp();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wo_shi_shang_jia;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
            //信息
            case R.id.iv_right:
                Toast.makeText(this, "暂未开通", Toast.LENGTH_SHORT).show();
                break;
            //我的商品
            case R.id.rl_woDeShangPin:
                Intent intent = new Intent(this, ShangPingHomeActivity.class);
                startActivity(intent);
                break;
            //卖家
            case R.id.rl_maiJiaJiaoLiu:
//                startActivity(new Intent(this, MaiJiaHomeActivity.class));
                Toast.makeText(this, "暂未开通", Toast.LENGTH_SHORT).show();
                break;
            //客服
            case R.id.rl_keFu:
                keFuDialog.show();
                break;
            //用户反馈
            case R.id.rl_yongHuFanKui:
//                startActivity(new Intent(this, YongHuFanKuiHomeActivity.class));
                Toast.makeText(this, "暂未开通", Toast.LENGTH_SHORT).show();
                break;
            //我的记录
            case R.id.rl_woDeJiLu:
                startActivity(new Intent(this, WoDeJiLvHomeActivity.class));
                break;
            //评论查看
            case R.id.rl_pingLunChaKan:
                startActivity(new Intent(this, PinLunHomeActivity.class));
                break;
            //我的收入
            case R.id.rl_woDeShouRu:
                startActivity(new Intent(this, WoDeShouRuActivity.class));
                break;
            //我的订单
            case R.id.rl_dingDan:
                startActivity(new Intent(this, DingDanHomeActivity.class).putExtra("shopname", tv_name02.getText().toString().trim()));
                break;
            //客服电话
            case R.id.tv_number:
                Intent telephoneIntent = new Intent();
                telephoneIntent.setAction(Intent.ACTION_DIAL);
                telephoneIntent.setData(Uri.parse("tel:" + "010-88909123"));
                startActivity(telephoneIntent);
                keFuDialog.dismiss();
                break;
        }

    }

    //客服Dialog
    private void keFuDialog() {

        keFuDialog = new Dialog(this, R.style.DialogTheme);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_ke_fu, null);
        keFuDialog.setContentView(dialogView);

        Window dialogWindow = keFuDialog.getWindow();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        dialogWindow.setGravity(Gravity.CENTER);

        tv_number = (TextView) dialogView.findViewById(R.id.tv_number);

        tv_number.setOnClickListener(this);
    }

    //获取数据 判断是否注册过或审核结状态
    private void initOkHttp() {

        String url = URLConstant.BASE_URL + "shop/showRegister";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String result = null;

                try {
                    result = response.body().string();
                    parseIsRegister(result);
                } catch (Exception e) {
                }


            }
        });
    }

    //解析是否注册过或审核结状态
    private void parseIsRegister(String result) {

        Gson gson = new Gson();
        WorkBean04 bean = gson.fromJson(result, WorkBean04.class);

        if (bean != null) {

            switch (bean.getState()) {

                //验证中
                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                //验证通过
                case 1:
                    mHandler.sendEmptyMessage(1);
                    break;

                //审核失败
                case 2:
                    mHandler.sendEmptyMessage(2);
                    break;

                //未注册
                case 3:
                    mHandler.sendEmptyMessage(3);
                    break;
                case 4:
                    mHandler.sendEmptyMessage(4);
                    break;

            }
        }
    }

    //请求网络 获取数据
    private void initShuJu() {

        String url = URLConstant.BASE_URL + "shop/show";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String result = null;

                try {
                    result = response.body().string();
                    parseShuJu(result);
                } catch (Exception e) {
                }


            }
        });
    }

    //解析商家数据
    private void parseShuJu(String result) {

        Gson gson = new Gson();
        WorkBean05 bean = gson.fromJson(result, WorkBean05.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(5);
                    break;

                case 1:

                    name01 = bean.getData().getShopkeeper();
                    name02 = bean.getData().getShopname();
                    telephone = bean.getData().getShoptele();
                    address = bean.getData().getAddress();
                    photoUrl = bean.getData().getShopImage();
                    mHandler.sendEmptyMessage(6);

                    break;
            }
        }

    }

}
